package DB

import (
	"baikBingo/pkg/Util"
	"fmt"
	"strconv"

	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

var (
	username = Util.Config("env.mysql.username")
	password = Util.Config("env.mysql.password")
	host     = Util.Config("env.mysql.host")
	port, _  = strconv.Atoi(Util.Config("env.mysql.port"))
	database = Util.Config("env.mysql.database")
	charset  = Util.Config("env.mysql.charset")
)

func Connect() *sqlx.DB {
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s", username, password, host, port, database, charset)
	Db, err := sqlx.Open("mysql", dsn)
	Db.SetMaxOpenConns(100) // 设置连接池最大连接数
	Db.SetMaxIdleConns(20)  // 设置连接池最大空闲连接数
	if err != nil {
		fmt.Printf("mysql connect failed, detail is [%v]", err.Error())
	}
	return Db
}
